<?php

namespace app\admin\controller;

use app\admin\model\Customer;
use app\admin\model\Track;
use think\Controller;
use think\Db;
use think\Request;

class Tracks extends Common
{
   protected $middleware = ['Auth'];

   /**
    * 显示资源列表
    *
    * @return \think\Response
    */
   public function index(Request $request)
   {
      //模糊查询
      $where = [];
      $where1 = [];
      $result = \config('admin.tracks');
      //按名称
      if ($search = input('get.title')) {
         $field = $request->param('search_field');
         $fieldName = $result[$field];  //获取搜索对应的名称
         if ($field == 'wang') {
            $where1[] = [$field, 'like', "%" . $search . "%"];
         } else {
            $where[] = [$field, 'like', "%" . $search . "%"];
         }
      }
      //按时间查询
      if ($request->has('date') and $request->param('date') != '') {
         $field = $request->param('search_field');
         $fieldDate = $result[$field]; //获取搜索对应的名称
         $time = explode(" ~ ", $request->param('date'));
         $start = $time[0] . ' 00:00:00';
         $end = $time[1] . ' 23:59:59';
         $where[] = [$field, 'between time', [$start, $end]];
      }
      //按是否进店搜索
      if ($is_come = input('get.is_come') and $is_come != '') {
         $where[] = ['is_come', '=', $is_come];
      }
      //按店铺搜索
      if ($dian = input('get.dian') and $dian != '') {
         $where1[] = ['dian', '=', $dian];
      }

      $dians = config('admin.dian');  //店铺
      $isComes = config('admin.is_come');  //是否进店
      $lifus = Db::table('clerks')->where('category_id', 3)->column('name');  //礼服师
      $huazhuangs = Db::table('clerks')->where('category_id', 4)->column('name');  //化妆师

      $customer_id = Customer::where('wang <> null or wang <> ""')->where($where1)->column('id');
      $tracks = Track::with(['customer' => function ($query) use ($where1) {
         $query->where($where1);
      }])
         ->whereIn('customer_id', $customer_id)
         ->where($where)
         ->order('create_at', 'desc')
         ->paginate(35, false, ['query' => request()->param()]);
      return view('Tracks/index', compact('fieldDate', 'fieldName', 'tracks', 'dians', 'isComes', 'lifus', 'huazhuangs'));
   }

   /***
    * 查看日志
    */
   public function showLog(Request $request)
   {
      //查出所有操作日志
      $trackLogs = Db::table('track_logs')
         ->where('customer_id', $request->param('log_id'))
         ->order('create_at', 'desc')
         ->select();

      return json($trackLogs);
   }


   /***
    * 多选删除
    */
   public function delete_all(Request $request)
   {
      Track::destroy($request->param('check_id'));
   }

   /***
    * 改变属性
    */
   public function change(Request $request)
   {
      $track = Track::find($request->id);
      $attr = $request->param('attr');
      $track->$attr = !$track->$attr;
      $track->save();

      //写入日志track_logs表
      $id = $track['customer_id'];
      $tracks = config('admin.tracks');
      $res = $track->$attr == 1 ? '是' : '否';
      $content = session('admin.name') . '更新客资' . $id . '[' . $tracks[$attr] . ']为' . $res;
      $this->log($id, $content);
   }


   /**
    * 更新信息
    */
   public function edit_track(Request $request)
   {
      $name = $request->param('name');
      $data['id'] = $request->param('id');
      $data[$name] = $request->param('val');

      $track = Track::find($data['id']);

      $role_id = Db::table('role_admin')->where('admin_id', session('admin.id'))->column('role_id');
      $customer_id = Db::table('customers')->where('wang', session('admin.name'))->column('id');
      //设置只能更改自己的权限
      if (in_array(5, $role_id)) {
         if (in_array($track['customer_id'], $customer_id)) {
            $track->save($data);
         } else {
            return json(['status' => 0, 'msg' => '无权操作']);
         }
      } else {
         $track->save($data);
      }

      //写入日志track_logs表
      $id = $track['customer_id'];
      $tracks = config('admin.tracks');
      $content = session('admin.name') . '更新客资' . $id . '[' . $tracks[$name] . ']为' . $data[$name];
      $this->log($id, $content);
   }


   /***
    * 写入日志表
    */
   public function log($id, $content)
   {
      $data['customer_id'] = $id;
      $data['name'] = session('admin.name');
      $data['log'] = $content;
      $data['create_at'] = date('Y-m-d H:i:s');

      Db::table('track_logs')->insert($data);
   }

   /***
    * 导出数据
    */
   public function export_track()
   {
      $urgents = config('admin.urgent'); //紧急客资

      $tracks = config('admin.tracks');
      $name = '客资追踪';
      $data = Track::with('customer')->order('customer_id', 'desc')->select();

      foreach ($data as &$item) {
         $item['is_urgent'] = $urgents[$item['customer']['is_urgent']];
         $item['is_phone'] = $item['customer']['is_phone'] == 1 ? '是' : '否';
         $item['is_come'] = 1 ? '是' : '否';
         $item['dian'] = $item['customer']['dian'];
      }

      export($tracks, $data, $name);
   }


}
